package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.bean.UserInfoResponse
import com.gexiaobao.pigeon.app.model.param.EditAddressParams
import com.gexiaobao.pigeon.app.model.param.EditMobileParams
import com.gexiaobao.pigeon.app.model.param.EditPassWordParams
import com.gexiaobao.pigeon.app.model.param.EditSexParams
import com.gexiaobao.pigeon.app.model.param.EditUserNameParams
import com.gexiaobao.pigeon.app.model.param.GetVerCodeParams
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

/**
 * @author:hxw
 * @date  :2023/1/10
 * @desc  :
 */
class PersonalInfoViewModel : BaseViewModel() {

    /** 个人信息 */
    val oldPassWord = StringObservableField()
    val newPassWord = StringObservableField()
    val newPassWord2 = StringObservableField()

    /** 修改手机号 */
    val newPhone = StringObservableField()
    val verCode = StringObservableField()
    val sendVerificationCode = StringObservableField()

    /** 修改地址 */
    val modifyAddress = StringObservableField()
    val modifyDetailAddress = StringObservableField()

    val gender = StringObservableField()
    val address = StringObservableField()
    val mobile = StringObservableField()
    val etUserName = StringObservableField()
    val userName = StringObservableField()
    val etRealName = StringObservableField()
    val realName = StringObservableField()

    /** 修改地址接收数据 */
    val editAddressResult: MutableLiveData<UiState> = MutableLiveData()

    /** 修改手机号接收数据 */
    val editMobileResult: MutableLiveData<UiState> = MutableLiveData()

    /** 修改性别接收数据 */
    val editGenderResult: MutableLiveData<UiState> = MutableLiveData()

    /** 修改用户名接收数据 */
    val editUserNameResult: MutableLiveData<UiState> = MutableLiveData()

    /** 修改密码 */
    val editPwdResult: MutableLiveData<UiState> = MutableLiveData()

    /** 修改头像 */
    val editHeadIconResult: MutableLiveData<UiState> = MutableLiveData()

    /** 用户数据 */
    val userInfoResult: MutableLiveData<UserInfoResponse> = MutableLiveData()

    /** 修改地址 */
    fun editAddress(params: EditAddressParams) {
        request({ apiService.editAddress(params) }, {
            val deleteState = UiState(isSuccess = true)
            editAddressResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            editAddressResult.value = deleteState
        })
    }

    /** 修改手机号 */
    fun editMobile(params: EditMobileParams) {
        request({ apiService.editMobile(params) }, {
            val deleteState = UiState(isSuccess = true)
            editMobileResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            editMobileResult.value = deleteState
        })
    }

    /** 修改性别 */
    fun editGender(params: EditSexParams) {
        request({ apiService.editGender(params) }, {
            val deleteState = UiState(isSuccess = true)
            editGenderResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            editGenderResult.value = deleteState
        })
    }

    /** 修改用户名 */
    fun editUserName(params: EditUserNameParams) {
        request({ apiService.editUserName(params) }, {
            val deleteState = UiState(isSuccess = true)
            editUserNameResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            editUserNameResult.value = deleteState
        })
    }

    /** 修改真实姓名 */
    fun editRealName(params: EditUserNameParams) {
        request({ apiService.editRealName(params) }, {
            val deleteState = UiState(isSuccess = true)
            editUserNameResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            editUserNameResult.value = deleteState
        })
    }

    /** 修改密码 */
    fun editPwd(params: EditPassWordParams) {
        request({ apiService.editPwd(params) }, {
            val deleteState = UiState(isSuccess = true)
            editPwdResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            editPwdResult.value = deleteState
        })
    }

    /** 获取验证码 */
    fun getVerCode(body: GetVerCodeParams) {
        request({ apiService.getVerCode(body) }, {
            //请求成功
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取头像 */
    fun editHeadIcon(imageFile: File) {
        val body: RequestBody =
            imageFile.asRequestBody("multipart/form-data".toMediaTypeOrNull()) //表单类型
        val builder = MultipartBody.Builder().setType(MultipartBody.FORM)//表单类型
        builder.addFormDataPart("image", imageFile.name, body) //添加图片数据，body创建的请求体
        val requestBody = builder.build()
        request({ apiService.editIcon(requestBody) }, {
            //请求成功
            val editHeadIcon = UiState(isSuccess = true)
            editHeadIconResult.value = editHeadIcon
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取单个用户信息 */
    fun getUserDetail() {
        request({ apiService.getUserDetail() }, {
            //请求成功
            userInfoResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

}